<?php 
/** 命名空间 */
namespace app\erl;

/**
//$rs = \app\erl\user_out(1, 5893);

//$rs = \app\erl\give_goods(1, 5893, 1002);

//$rs = \app\erl\give_gold(1, 5893, 100,false);

//$rs = \app\erl\give_exp(1, 5893, 66);

//$rs = \app\erl\give_rmb(1, 5893, 5);

//$rs = \app\erl\give_point(1, 5893, 50);

//$rs = \app\erl\give_pay(1, 5893, true);

//$rs = \app\erl\check_yrl(1);

//$rs = \app\erl\reload(1);

//$rs = \app\erl\reboot(1,true);

//$rs = \app\erl\online(1);

//$rs = \app\erl\send_mail(1, array(5893), "测试", "测试内容",1,2005);

//$rs = \app\erl\send_mail_all(1, "测试2", "测试内容3",1,2005,8);
 */
/**
 * 重起服务器
 * @param uint $sid
 * @param bool $is_now  true:马上重启  false:先发通知，5分钟后重起服务器
 */
function reboot($sid,$is_now=false)
{
    $is_now = $is_now?'true':'false';
	$data	= "[{$is_now}]";
	return private_erlang_call('gc_server','reboot',$sid,$data);
}
/**
 * 重新加载ETS
 * @param uint $sid
 */
function reload($sid)
{
	$data	= "[]";
	return private_erlang_call('gc_server','reload',$sid,$data);
}

/**
 * 校正Yrl数据
 * @param uint $sid
 */
function check_yrl($sid)
{
	$data	= "[]";
	return private_erlang_call('gc_server','check_yrl',$sid,$data);
}
/**
 * 得到服务器当前在线
 * @param uint $sid
 */
function online($sid)
{
    $data	= "[]";
    return private_erlang_call('gm_api_master','online',$sid,$data);
}
/**
 * 踢玩家下线
 * @param uint $sid
 * @param uint $uid
 * @return array
 *     0 => boolean true
 *     1 => string 'true' (length=4)
 */
function user_out($sid,$uid)
{
	$data	= "[{$uid}]";
	return private_erlang_call('gm_api_master','user_out',$sid,$data);
}

/**
 * 得到玩家，现在数据
 * @param uint $sid
 * @param uint $uid
 * @return json 背包等所有玩家的数据
 */
function user_info($sid,$uid)
{
	$data	= "[{$uid}]";
	return private_erlang_call('gm_api_master','user_info',$sid,$data);
}

/**
 * 冲值成功
 * @param uint $sid
 * @param uint $uid
 * @param uint $is_first   true:第一次  false:第二次等
 * @return Ambigous <\app\erl\Ambigous, multitype:, multitype:boolean string >
 */
function give_pay($sid,$uid,$is_first=false)
{
    $is_first = $is_first?'true':'false';
	$data     = "[{$sid},{$uid},{$is_first}]";
	return private_erlang_call('gm_api_master','give_pay',$sid,$data);
}
/**
 * GM加经验
 * @param uint $sid
 * @param uint $uid
 * @param uint $exp
 * @return Ambigous <\app\erl\Ambigous, multitype:, multitype:boolean string >
 */
function give_exp($sid,$uid,$exp)
{
	$data	= "[{$uid},{$exp}]";
	return private_erlang_call('gm_api_master','give_exp',$sid,$data);
}

/**
 * GM加元宝 (改用 \app\hy\user_pay($db, $sid, $uid, $type, $pid, $oid, $rmb, $point, $remark))
 * @param uint $sid
 * @param uint $uid
 * @param uint $rmb
 */
// function give_rmb($sid,$uid,$rmb)
// {
//     $data	= "[{$uid},{$rmb}]";
//     return private_erlang_call('gm_api_master','give_rmb',$sid,$data);
// }

/**
 * GM加礼劵 （改用 \app\hy\user_pay($db, $sid, $uid, $type, $pid, $oid, $rmb, $point, $remark) ）
 * @param uint $sid
 * @param uint $uid
 * @param uint $point
 */
// function give_point($sid,$uid,$point)
// {
//     $data	= "[{$uid},{$point}]";
//     return private_erlang_call('gm_api_master','give_point',$sid,$data);
// }

/**
 * GM加金币
 * @param uint $sid
 * @param uint $uid
 * @param uint $gold
 * @param bool $is_bind  true:邦定 false:没邦定
 */
function give_gold($sid,$uid,$gold,$is_bind=true)
{
    $is_bind = $is_bind?'true':'false';
	$data	 = "[{$uid},{$gold},{$is_bind}]";
	return private_erlang_call('gm_api_master','give_gold',$sid,$data);
}

/**
 * GM给物品
 * @param uint $sid
 * @param uint $uid
 * @param uint $goods_id
 * @param uint $count
 * @param uint $streng
 * @param uint $name_color 
 * @param uint $bind
 * @param uint $expiry_type
 * @param uint $expiry
 */
function give_goods($sid,$uid,$goods_id,$count=1,$streng=0,$name_color=1,$bind=1,$expiry_type=0,$expiry=0)
{
	$goods	= private_goods_give($goods_id,$count,$streng,$name_color,$bind,$expiry_type,$expiry);
	$data	= "[{$uid},{$goods}]";
	return private_erlang_call('gm_api_master','give_goods',$sid,$data);
}

/**
 * 向一个或多个玩家发放邮件
 * @param string $sid
 * @param array $uids  
 * @param string $title
 * @param string $content
 * @param uint $gold
 * @param uint $goods_id
 * @param uint $count
 * @param uint $streng
 * @param uint $name_color
 * @param uint $bind
 * @param uint $expiry_type
 * @param uint $expiry
 */
function send_mail($sid,$uids,$title,$content,$gold=0,$goods_id=0,$count=1,$streng=0,$name_color=1,$bind=1,$expiry_type=0,$expiry=0)
{
    $uids    = "[".implode(",", $uids)."]";
    $title   = "<<".private_string_binary($title).">>";
    $content = "<<".private_string_binary($content).">>";
    $gold    = (int)$gold;
    $goods   = 0;
    if($goods_id)
    {
        $goods	= private_goods_give($goods_id,$count,$streng,$name_color,$bind,$expiry_type,$expiry);
    }    
    $data	= "[{$sid},{$uids},{$title},{$content},{$gold},{$goods}]"; 
    return private_erlang_call('gm_api_master','send_mail',$sid,$data); 
}
/**
 * 全服发邮件
 * @param uint $sid
 * @param string $title
 * @param string $content
 * @param uint $gold
 * @param uint $goods_id
 * @param uint $count
 * @param uint $streng
 * @param uint $name_color
 * @param uint $bind
 * @param uint $expiry_type
 * @param uint $expiry
 */
function send_mail_all($sid,$title,$content,$gold=0,$goods_id=0,$count=1,$streng=0,$name_color=1,$bind=1,$expiry_type=0,$expiry=0)
{
    $title   = "<<".private_string_binary($title).">>";
    $content = "<<".private_string_binary($content).">>";
    $gold    = (int)$gold;    
    $goods   = 0;
    if($goods_id)
    {
        $goods	= private_goods_give($goods_id,$count,$streng,$name_color,$bind,$expiry_type,$expiry);
    }    
    $data	= "[{$sid},{$title},{$content},{$gold},{$goods}]";
    return private_erlang_call('gm_api_master','send_mail_all',$sid,$data);
}

/**
 * 发送公告
 * @param uint $sid
 * @param uint $id
 * @param uint $interval     秒
 * @param uint $begin_time   秒
 * @param uint $end_time     秒
 * @param string $content
 */
function send_notice($sid,$id,$interval,$begin_time,$end_time,$show_time,$content)
{
    $content = str_replace(array("\n","\r"), '', $content);
    $content = "<<".private_string_binary($content).">>";
    $data    = "[{$id},{$interval},{$begin_time},{$end_time},{$show_time},{$content}]";
    return private_erlang_call('gm_api_master','send_notice',$sid,$data);
}
/**
 * 删除公告
 * @param uint $sid
 * @param uint $id
 */
function send_notice_del($sid,$id)
{
    $data    = "[{$id}]";
    return private_erlang_call('gm_api_master','send_notice_del',$sid,$data);
}
/**
 * 开放系统
 * @param uint $sid
 * @param uint $uid
 * @param uint $task_id
 */
function open_sys($sid,$uid,$task_id)
{
    $data    = "[{$uid},{$task_id}]";
    return private_erlang_call('gm_api_master','open_sys',$sid,$data);
}
/**
 * 统一调用调用
 * @param uint   $sid
 * @param string $fun
 * @param string $arg_data
 * @return Ambigous <multitype:, multitype:boolean string >
 */
function private_erlang_call($mod,$fun,$sid,$arg_data) 
{
	$time = time(); 
	$md5  = md5($sid.'_'.$arg_data.'_'.$time.'_'.Const_Key_Conn_Private);
	//echo $sid.'_'.$arg_data.'_'.$time.'_'.Const_Key_Conn_Private,"<br />";
	//echo Const_Serv_Host;
	//exit();
	return \ounun\Http::Erlang($mod,$fun,"{{$sid},\"{$md5}\",{$time},{$arg_data}}",Const_Serv_Host,Const_Serv_PortMaster);
}

/**
 * 得到give数据元组
 * @param uint $goods_id 	物品ID
 * @param uint $count    	数量
 * @param uint $streng	  	强化等级
 * @param uint $name_color  物品名称的颜色
 * @param uint $bind		是否绑定(0:不绑定 1:绑定)
 * @param uint $expiry_type 有效期类型，0:不失效，1：秒，  2：天，请多预留几个以后会增加
 * @param uint $expiry		有效期，到期后自动消失，并发系统邮件通知
 */
function private_goods_give($goods_id,$count,$streng,$name_color,$bind,$expiry_type,$expiry)
{
	// {give,2005,1,0,1,1,0,0}
	return "{give,{$goods_id},{$count},{$streng},{$name_color},{$bind},{$expiry_type},{$expiry}}";
}

/**
 * 
 */
function private_string_binary($string)
{
    $i = 0;
    $number = array();
    while (isset($string{$i})) 
    {
        $number[]= ord($string{$i});
        ++$i;
    }
    return implode(',', $number);
}

